﻿using StudentInfoSystem.Data.StudentInfoSystem;
using System;
using System.Windows.Forms;
using StudentInfoSystem.Data;
using System.Text;

namespace StudentInfoSystem.View
{
    public partial class MainForm : Form
    {
        public UserStatus UserStatus { get; private set; }

        public MainForm()
        {
            InitializeComponent();
            MakeControlsInactive();
            menuItemLogin.Text = "Login";
            UserStatus = UserStatus.Anonymos;
        }

        private void label1_Click(object sender, EventArgs e)
        {
        }

        private void label1_Click_1(object sender, EventArgs e)
        {
        }

        private void label1_Click_2(object sender, EventArgs e)
        {
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show("Are you sure that you want to quit?", "Closing", MessageBoxButtons.YesNo) == DialogResult.No)
            {
                e.Cancel = true;
            }
        }

        private void DeleteEnteredInformation()
        {
            textBoxFaculty.Text = String.Empty;
            textBoxSubject.Text = String.Empty;
            comboBoxOks.ResetText();
            comboBoxState.ResetText();
            textBoxFacultyNumber.Text = String.Empty;
            numericUpDownCourse.ResetText();
            textBoxStream.Text = String.Empty;
            textBoxGroup.Text = String.Empty;
            textBoxFirstName.Text = String.Empty;
            textBoxMiddleName.Text = String.Empty;
            textBoxLastName.Text = String.Empty;
        }

        //TO DO:
        private void DisplayStudentInformation()
        {
            MakeControlsActive();
            textBoxFaculty.Text = "FKSU";
            textBoxSubject.Text = "KST";
            comboBoxOks.Text = "Bachelor";
            comboBoxState.Text = "Regular";
            textBoxFacultyNumber.Text = "121208260";
            numericUpDownCourse.Value = 3;
            textBoxStream.Text = "1";
            textBoxGroup.Text = "58";
            textBoxFirstName.Text = "Anton";
            textBoxMiddleName.Text = "Stanislavov";
            textBoxLastName.Text = "Angelov";
        }

        private void DisplayStudentInformation(Student studentInfo)
        {
            MakeControlsActive(); 
            textBoxFaculty.Text = studentInfo.Faculty;
           
            textBoxSubject.Text = studentInfo.Subject;
            comboBoxOks.Text = studentInfo.OKS.ToString();
            comboBoxState.Text = studentInfo.State.ToString();
            textBoxFacultyNumber.Text = studentInfo.FacNumber;
            numericUpDownCourse.Value = studentInfo.Course;
            textBoxStream.Text = studentInfo.Stream;
            textBoxGroup.Text = studentInfo.Group.ToString();
            textBoxFirstName.Text = studentInfo.FirstName;
            textBoxMiddleName.Text = studentInfo.MiddleName;
            textBoxLastName.Text = studentInfo.LastName;
        }

        private void MakeControlsInactive()
        {
            groupBoxStudentInfo.Enabled = false;
            groupBoxPersonalData.Enabled = false;
            openPicture.Visible = false;
            settingsMenu.Visible = false;
        }

        private void MakeControlsActive()
        {
            groupBoxStudentInfo.Enabled = true;
            groupBoxPersonalData.Enabled = true;
            openPicture.Visible = true;
            settingsMenu.Visible = true;
        }

        private void loginMenuItem_Click(object sender, EventArgs e)
        { 
            switch (menuItemLogin.Text)
            {
                case "Login":
                    LoginForm login = new LoginForm();
                    login.ShowDialog();
                    if (login.DialogResult == DialogResult.OK)
                    {
                        Student student = UserData.GetStudentByFacNumber(login.user.FacNumber);
                        if (student != null)
                        {
                            DisplayStudentInformation(student);
                        }
                        else
                        {
                            DisplayStudentInformation();
                        }
                        MakeControlsActive();
                        menuItemLogin.Text = "Logout";
                    }
                    else
                    {
                        MakeControlsInactive();
                        menuItemLogin.Text = "Login";
                    }
                    break;
                case "Logout":
                    menuItemLogin.Text = "Login";
                    DeleteEnteredInformation();
                    MakeControlsInactive();
                    break;
                default:
                    break;
            }
        }

        private void ProcessUserStatus()
        {
            switch (UserStatus)
            {
                case UserStatus.Anonymos:
                    UserStatus = UserStatus.Student;
                    break;
                case UserStatus.Student:
                    break;
                case UserStatus.Admin:
                    break;
                default:
                    break;
            }
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
        }

        private void menuItemExit_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure that you want to quit?", "Closing", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                this.Dispose();
            }
        }

        private void aboutMenuItem_Click(object sender, EventArgs e)
        {
            AboutBox about = new AboutBox();
            about.ShowDialog();
        }

        private void openFileDialog1_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
        {
        }

        private void systemMenuStudent_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
        }

        private void LoadStudentPicture()
        {
            if (openPictureDialog.ShowDialog() == DialogResult.OK)
            {
                pbStudentPhoto.ImageLocation = openPictureDialog.FileName;               
            }
        }

        private void tabGroupBoxPicture_Enter(object sender, EventArgs e)
        {
        }

        private void openPictureDial_Click(object sender, EventArgs e)
        {
            LoadStudentPicture();
        }

        private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string text = GenerateInfoText();
            InfoForm infoForm = new InfoForm(text);
            infoForm.Show(); 
        }

        private string GenerateInfoText()
        {
            StringBuilder text = new StringBuilder();
            text.AppendLine("Assurance!");
            text.AppendLine(textBoxFirstName.Text);
            text.AppendLine(textBoxMiddleName.Text);
            text.AppendLine(textBoxLastName.Text);
            text.AppendLine(textBoxFacultyNumber.Text);
            text.AppendLine(textBoxGroup.Text);
            text.AppendLine(textBoxStream.Text);
            text.AppendLine(textBoxSubject.Text);
            return text.ToString();
        }
    }
}